---
title: Memory with MongoDB
---

## Code

```python cookbook/memory/db/mem-mongodb-memory.py
from agno.agent import Agent
from agno.db.mongo import MongoDb

# Setup MongoDb
db_url = "mongodb://localhost:27017"

db = MongoDb(db_url=db_url)

agent = Agent(
    db=db,
    enable_user_memories=True,
)

agent.print_response("My name is John Doe and I like to play basketball on the weekends.")
agent.print_response("What's do I do in weekends?")
```

## Usage

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Set environment variables">
    ```bash
    export OPENAI_API_KEY=xxx
    ```
  </Step>

  <Step title="Install libraries">
    ```bash
    pip install -U agno openai pymongo
    ```
  </Step>

  <Step title="Run Example">
    <CodeGroup>
    ```bash Mac/Linux
    python cookbook/memory/db/mem-mongodb-memory.py
    ```

    ```bash Windows
    python cookbook/memory/db/mem-mongodb-memory.py
    ```
    </CodeGroup>
  </Step>
</Steps>
